package com.yihukurama.sysbase.framework.domainservice;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.yihukurama.base.app.exception.TipsException;
import com.yihukurama.base.app.utils.EmptyUtil;
import com.yihukurama.base.app.utils.LogUtil;
import com.yihukurama.base.app.utils.TransferUtils;
import com.yihukurama.base.framework.web.dto.Request;
import com.yihukurama.base.framework.web.dto.Result;
import com.yihukurama.sysbase.framework.domain.User;
import com.yihukurama.sysbase.framework.domain.Userrole;
import com.yihukurama.sysbase.framework.entity.RoleEntity;
import com.yihukurama.sysbase.framework.entity.UserEntity;
import com.yihukurama.sysbase.framework.entity.UserroleEntity;
import com.yihukurama.sysbase.framework.mapper.RoleMapper;
import com.yihukurama.sysbase.framework.mapper.UserMapper;
import net.bytebuddy.pool.TypePool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yihukurama.base.framework.service.domainservice.CrudService;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * 说明： Userrole的领域服务
 * @Author: yihukurama
 */
@Service
public class UserroleService extends CrudService<UserroleEntity> {


    @Autowired
    UserMapper userMapper;
    @Autowired
    RoleMapper roleMapper;

    @Override
    public int creates(List<UserroleEntity> list) throws TipsException {

        if (EmptyUtil.isEmpty(list)){
            throw new TipsException("无创建数据");
        }


        List<Userrole> userRoleList = TransferUtils.transferEntityList2DomainList(list,Userrole.class);
        list = TransferUtils.transferDomainList2EntityList(userRoleList,UserroleEntity.class);
        UserroleEntity deleteUserroleEntity = new UserroleEntity();
        deleteUserroleEntity.setIds(userRoleList.get(0).getIds());

        try {
            removes(deleteUserroleEntity);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

        return super.creates(list);
    }

    @Override
    public Result list(UserroleEntity userroleEntity, Integer page, Integer limit) throws TipsException {

        Result userroleEntityList = super.list(userroleEntity,page,limit);

        List<UserroleEntity> userroleList = new ArrayList<>();

        for (UserroleEntity tmp:(List<UserroleEntity>)userroleEntityList.getData()
        ) {
            Userrole userrole = TransferUtils.transferEntity2Domain(tmp,Userrole.class);

            RoleEntity roleEntity = new RoleEntity();
            roleEntity.setId(tmp.getRoleId());
            roleEntity = roleMapper.selectByPrimaryKey(roleEntity);
            userrole.setRole(roleEntity);

            UserEntity userEntity = new UserEntity();
            userEntity.setId(tmp.getUserId());
            userEntity = userMapper.selectByPrimaryKey(userEntity);
            userrole.setUser(userEntity);

            userroleList.add(userrole);

        }

        return Result.successed(userroleList);
    }
}
